/**
 * 加载场景
 */

import Define, { CustomAdType } from "./SdkTools/Define";
import gameCheckLogin from "./SdkTools/gameCheckLogin";
import gameNetWorkState from "./SdkTools/gameNetWorkState";
import gamePrefabMgr from "./SdkTools/gamePrefabMgr";
import gameShareAdsVideo from "./SdkTools/gameShareAdsVideo";
import storage from "./SdkTools/storage";
import TipsManager from "./SdkTools/TipsManager";
import SDKManager from "./plat/SDKManager";
import Utils from "./SdkTools/Utils";
import TopUi from "./TopUi";


const {ccclass, property} = cc._decorator;

@ccclass
export default class LoadScene extends cc.Component {

    @property(cc.ProgressBar)
    loadbar:cc.ProgressBar = null

    // LIFE-CYCLE CALLBACKS:
    private loadOver:boolean = false

    onLoad () {
        cc.debug.setDisplayStats(false)
        this.loadOver = false
        let self = this

        if(cc.sys.platform == cc.sys.WECHAT_GAME){

            //@ts-ignore
            const updateManager = wx.getUpdateManager()
            updateManager.onCheckForUpdate(function (res) {
                // 请求完新版本信息的回调
                console.log(res.hasUpdate)
            })
              
            updateManager.onUpdateReady(function () {
                //@ts-ignore
                wx.showModal({
                  title: '更新提示',
                  content: '新版本已经准备好，是否重启应用？',
                  success: function (res) {
                    if (res.confirm) {
                      // 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
                      updateManager.applyUpdate()
                    }
                  }
                })
            })

            //@ts-ignore
            wx.showShareMenu({
                success: (res) => {
                    console.log('开启被动转发成功!');
                },
                fail: (res) => {
                    console.log(res);
                    console.log('开启被动转发失败!');
                }
            });

            // 获取当前棋局oneChess信息，JSON.stringfy()后传入query
            //@ts-ignore
            wx.onShareAppMessage(() => {
                // return {
                //     title: '', 
                //     imageUrl: '',        // 分享图片要放在 wechatgame/res/raw-assets 路径下
                //     query: ''  // query最大长度(length)为2048
                // }
                
                return {
                    title: '80%老同学都卡在这里了，难道这关就这么难了吗？', 
                    imageUrl: 'https://mmocgame.qpic.cn/wechatgame/fIiaXSY0ANicTRfDr395BbEDT0R54yO9lsUWiasL5Xg8XIcrwUOvqOA0vt6hMOp29tu/0',        // 分享图片要放在 wechatgame/res/raw-assets 路径下
                    query: ''  // query最大长度(length)为2048
                }
            });
        }

        TipsManager.Instance.Initial() //初始化
        gamePrefabMgr.Instance.Initial()
        SDKManager.getInstance().onInit();
        gameShareAdsVideo.Instance.chaPingAds_Load()
        gameShareAdsVideo.Instance.motivational_Video_Load()
        gameShareAdsVideo.Instance.createCustomAd(CustomAdType.Shu)
        gameShareAdsVideo.Instance.createCustomAd(CustomAdType.More)
        gameCheckLogin.gameLogin(false)

        storage.Instance.eneryTime = Define.eneryTime

        cc.game.on('addgold', ()=>{
            let topuisc:TopUi = null
            let sc = cc.find('Canvas/top').getComponent(TopUi)
            if(sc){
                topuisc = sc
            }
            let over = cc.find('Canvas/GameOver/top')
            if(over){
                let sc2 = over.getComponent(TopUi)
                if(sc2){
                    topuisc = sc2
                }
            }
            if(topuisc){
                topuisc.setGold()
            }
        })

        cc.game.on('addengry',()=>{
            let topuisc:TopUi = null
            let sc = cc.find('Canvas/top').getComponent(TopUi)
            if(sc){
                topuisc = sc
            }
            let over = cc.find('Canvas/GameOver/top')
            if(over){
                let sc2 = over.getComponent(TopUi)
                if(sc2){
                    topuisc = sc2
                }
            }
            if(topuisc){
                topuisc.setTiLi()
            }
        })

        gameNetWorkState.Instance.getGameOpenState(()=>{})
    }

    start () {

    }

    update (dt) {
        if(this.loadOver){
            return
        }
        this.loadbar.progress = gamePrefabMgr.Instance.jilv/gamePrefabMgr.Instance.allResNum
        if(this.loadbar.progress >= 1){
            this.loadOver = !0
            cc.director.loadScene('StartScene')
        }
    }
}
